1.getBoundingClientRect
const triggers = document.querySelectorAll('a');
const highlight = document.createElement('span');
hightight.classList.add('highlight');
doccument.body.append(highlight);
function highlightLink(){
const linkCoords = this.getBonudingClientRect();
const coords={
width:linkCoords.width,
height:linkCoords.height,
top:linkCoords.top + window.scrollY,
left:linkCoords.left +window.scrollX
};
higtlight.style.width=`${coords.width}px`;
higtlight.style.height=`${coords.height}px`;
higtlight.style.transform=`translate(${coords.left}px, ${coords.top}px)`
}
tiggers.forEach(a => a.addEventListener('mouseenter',highlightLink))
參考資料來源: